package com.jeesite.modules.ocr.utils;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Base64;
import java.util.HashMap;

import com.jeesite.common.io.FileUtils;
import com.jeesite.modules.file.utils.FileUploadUtils;
import com.jeesite.modules.ocr.base.IOcrBaseInterface;
import com.jeesite.modules.ocr.utils.AIKitsOCRUtils.OCRAPI;

import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okio.Buffer;

/**
 * 华为 OCR 调用样例集 文档详见：22-人工智能套件(AIKits) 8.3.1 API参考(for 华为云Stack 8.3.1) 01.pdf
 * 
 * @author 姚海陆,长春叭哥
 * @version 2024-07-28
 */
public class AIKitsOCRUtils implements IOcrBaseInterface {

	/**
	 * 通用ocr识别验证（该接口需要登录管网vpn） http://ip:port/v2/123/ocr/general-text
	 * 
	 * @param args
	 * @throws IOException
	 */
	public static void main(String[] args) throws IOException {
		// 先判断发票的分类
		// 定额发票判断
		// 增值税 发票判断
		// 通用ocr 发票的判断

		String base64 = AIKitsOCRDemoUtils.convertImageToBase64("D:\\History\\2024\\52-侯成力-大模型开发\\01-15天\\13-样本集\\假样本-01.png");
		String json = "{\"image\":\"" +base64+ "\",  \"detect_direction\":true,  \"quick_mode\":false }";
		String result03=AIKitsOCRUtils.aiKitOCR(OCRAPI.通用文字识别,"123",json);
		System.out.println(result03);
		//System.out.println(base64);
		//String json = "{\"image\":\"" + base64 + "\"}";
		//String result01 = aiKitOCR(OCRAPI.定额发票识别, "123", json);
		//System.out.println(result01);
		// String result02 = aiKitOCR(OCRAPI.通用文字识别, "123", json);
		// System.out.println(result02);
		// String json通用表格识别 = "{\"image\":\"" + base64 + "\"}";
		// String result03=aiKitOCR(OCRAPI.通用表格识别,"123",json通用表格识别);
		// System.out.println(result03);

	}

	public static final String END_POINT = "http://ip:port/";
	public static final String API_VERSION = "v2";

	public enum OCRAPI {
		通用表格识别("general-table"), 增值税发票识别("vat-invoice"), 发票验真("invoice-verification"), 通用文字识别("general-text"),
		智能文档解析("smart-document-recognizer"), 防疫健康码识别("health-code"), 网络图片识别("web-image"), 定额发票识别("quota-invoice"),
		身份证识别("id-card"), 手写文字识别("handwriting"), 印章识别("seal"), 行驶证识别("vehicle-license"),
		道路运输证识别("transportation-license"), 出租车发票识别("taxi-invoice"), 智能分类识别("auto-classification"),
		车辆通行费发票识别("toll-invoice"), 机动车销售发票识别("mvs-invoice"), 车牌识别("general-table"), 飞机行程单识别("mvs-invoice"),
		营业执照识别("business-license"), 驾驶证识别("driver-license"), 名片识别("business-card"), 火车票识别("train-ticket"),
		VIN码识别("general-table"), 护照识别("passport"), 银行卡识别("recognizeBankcard"), 保险单识别("recognizeInsurancePolicy"),
		财务报表识别("financial-statement"), 道路运输从业资格证识别("transportation-qualification-certificate"),
		电子面单识别("waybill-electronic"), 核酸检测记录识别("pcr-test-record"), 自定义模板OCR("custom-template"),
		泰文身份证识别("thailand-id-card"), 泰国车牌识别("thailand-license-plate"), 车辆合格证识别("vehicle-certificate"),
		承兑汇票识别("acceptance-bill"), 不动产证识别("real-estate-certificate"), 哥伦比亚身份证识别("colombia-id-card"),
		户口本识别("household-register"), 银行回单识别("bank-receipt");
		private final String url;

		OCRAPI(String url) {
			this.url = url;
		}

		public String getUrl() {
			return url;
		}
	}

	public static String getAPIHost(String project_id) {
		return END_POINT + API_VERSION + "/" + project_id + "/ocr/";
	}

	public static String getAIPURL(OCRAPI functionName, String project_id) {
		return getAPIHost(project_id) + functionName.getUrl();
	}

	public static String getAuthorizationToken() {
		// 利用缓存定时更新获取值
		return "MIIEiwYJKoZIhvcNAQcCoIIEfDCCBHgCAQExDTALBglghkgBZQMEAgEwggLsBgkqhkiG9w0BBwGgggLdBIIC2XsidG9rZW4iOnsiZXhwaXJlc19hdCI6IjIwMjQtMDctMTJUMDY6NTU6MTguNDQyMDAwWiIsIm1ldGhvZHMiOlsicGFzc3dvcmQiXSwiY2F0YWxvZyI6W10sInJvbGVzIjpb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ressWarnings("deprecation")
	public static String aiKitOCR(OCRAPI functionName, String project_id, String json) throws IOException {
		OkHttpClient client = new OkHttpClient().newBuilder().build();
		MediaType mediaType = MediaType.get("application/json; charset=utf-8");
		RequestBody body = RequestBody.create(mediaType, json);
		Request request = new Request.Builder().url(getAIPURL(functionName, project_id)).method("POST", body)
				.addHeader("X-Auth-Token", getAuthorizationToken())
				.addHeader("User-Agent", "Apifox/1.0.0 (https://apifox.com)")
				.addHeader("Content-Type", "application/json").addHeader("Accept", "*/*")
				.addHeader("Host", "ip:port").addHeader("Connection", "keep-alive").build();
		// 发送请求并处理响应
		Response response = null;
		try {
			response = client.newCall(request).execute();
			if (!response.isSuccessful())
				throw new IOException("Unexpected code " + response);
			// 处理响应
			return response.body().string();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (response != null) {
				response.body().close();
			}
		}
		return "";
	}

}
